package com.cn.medicalcare.controller.backstageservlet.news;

import com.cn.medicalcare.customtool.JsonTool;
import com.cn.medicalcare.pojo.AllPage;
import com.cn.medicalcare.pojo.News;
import com.cn.medicalcare.pojo.Page;
import com.cn.medicalcare.service.NewsService;
import com.cn.medicalcare.service.serviceimpl.NewsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.Map;

@WebServlet("/backstage/news/getnewsinpage.json")
public class NewsInPageServlet extends HttpServlet {

    private NewsService newsService = NewsServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String category_name= req.getParameter("category_name");
        String title = req.getParameter("title");
        String summary = req.getParameter("summary");
        String newsdate = req.getParameter("newsdate");
        String survival = req.getParameter("survival");
        String istop = req.getParameter("istop");

        News news = new News();
        Map<String, Object> map = new LinkedHashMap<>();

        try {
            if (survival != null && survival.length() > 0)
                news.setSurvival(Integer.valueOf(survival));
            if (istop != null && istop.length() > 0)
                news.setIstop(Integer.valueOf(istop));
            if (newsdate != null && newsdate.length() > 0) {
                news.setNewsdate(newsdate);
            }
            news.setCategory_name(category_name).setTitle(title).setSummary(summary);
            int pagenum = Integer.valueOf(req.getParameter("page"));
            int limit = Integer.valueOf(req.getParameter("limit"));
            Page page = null;
            if (pagenum > 0 && limit > 0)
                page = new Page((pagenum - 1) * limit, limit);
            AllPage<News> allPage = new AllPage<>(news, page);
            map.put("status", 200);
            map.put("data", newsService.selectNewsInPage(allPage));
            map.put("count", newsService.selectNewsCount(news));
        } catch (Exception e) {
            map.put("status", 500);
        }
        resp.getWriter().print(JsonTool.toJson(map));
    }

}
